package com.fingard.dsp.bank.directbank.ccb02;

import com.fingard.dsp.bank.directbank.bankInterface.IBalanceCheck;
import com.fingard.dsp.bank.reqrespobj.balancecheck.ReqBalCheckQry;
import com.fingard.dsp.bank.reqrespobj.balancecheck.RetAtsBalCheckQry;
import com.fingard.dsp.bank.reqrespobj.balancecheck.detail.RetAtsBalCheckDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CCB02BalanceCheck extends CCB02Base implements IBalanceCheck {
    @Override
    public RetAtsBalCheckQry d_7508_BalCheck(ReqBalCheckQry p_req7508) throws Exception {
        //余额对账查询返回ATS的对象
        RetAtsBalCheckQry retAts = new RetAtsBalCheckQry(p_req7508.reqHeader, p_req7508.actNum);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            int startPage = 1;//查询起始页
            int countPage = 0;//查询总页数
            do {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                CCB02Header bankHeader = new CCB02Header();
                bankHeader.TX_CODE = "6WA001";
                bankHeader.REQUEST_SN = p_req7508.reqHeader.transSeqID;
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("TX_INFO");
                xmlWriter.writeElementString("PAGE_NO", startPage + "");
                xmlWriter.writeStartElement("LIST");
                xmlWriter.writeElementString("ACCT_NO", p_req7508.actNum);//对账账号
                Date dateFrom = p_req7508.getYearMonth();
                xmlWriter.writeElementString("BEGIN_DATE", new SimpleDateFormat("yyyyMM").format(dateFrom));//对账开始日期 格式：YYYYMM
                xmlWriter.writeElementString("END_DATE", new SimpleDateFormat("yyyyMM").format(dateFrom));//对账结束日期 格式：YYYYMM
                xmlWriter.writeEndDocument();

                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
//                String[] tmpRetBkStr = {"", "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\" ?>  \n" +
//                        "<TX>  \n" +
//                        "  <REQUEST_SN>请求序列码</REQUEST_SN>  \n" +
//                        "  <CUST_ID>客户号</CUST_ID>  \n" +
//                        "  <TX_CODE>6W4001</TX_CODE>  \n" +
//                        "  <RETURN_CODE>000000</RETURN_CODE>  \n" +
//                        "  <RETURN_MSG>返回码说明</RETURN_MSG>  \n" +
//                        "  <LANGUAGE>CN</LANGUAGE>  \n" +
//                        "  <TX_INFO>  \n" +
//                        "      <PAGE_NO>1</PAGE_NO>  \n" +
//                        "      <TOT_PAGE_NUM>2</TOT_PAGE_NUM>  \n" +
//                        "    <LIST>  \n" +
//                        "      <BILL_NO>cx0001</BILL_NO>  \n" +
//                        "      <ACCT_NAME>cx</ACCT_NAME>  \n" +
//                        "      <DEP_TYPE>52</DEP_TYPE>  \n" +
//                        "      <ACCT_NO>99999999999999</ACCT_NO>  \n" +
//                        "      <COIN_TYPE>01</COIN_TYPE>  \n" +
//                        "      <CURR_BAL>123.12</CURR_BAL>  \n" +
//                        "      <CHECK_FLAG>2</CHECK_FLAG>  \n" +
//                        "      <LAST_BILL_DATE>20200201</LAST_BILL_DATE>  \n" +
//                        "      <BILL_DATE>20200205</BILL_DATE>  \n" +
//                        "    </LIST>  \n" +
//                        "\t<LIST>  \n" +
//                        "      <BILL_NO>cx0002</BILL_NO>  \n" +
//                        "      <ACCT_NAME>cx</ACCT_NAME>  \n" +
//                        "      <DEP_TYPE>52</DEP_TYPE>  \n" +
//                        "      <ACCT_NO>888888888888888</ACCT_NO>  \n" +
//                        "      <COIN_TYPE>01</COIN_TYPE>  \n" +
//                        "      <CURR_BAL>123.12</CURR_BAL>  \n" +
//                        "      <CHECK_FLAG>2</CHECK_FLAG>  \n" +
//                        "      <LAST_BILL_DATE>20200201</LAST_BILL_DATE>  \n" +
//                        "      <BILL_DATE>20200205</BILL_DATE>  \n" +
//                        "    </LIST> \n" +
//                        "  </TX_INFO>    \n" +
//                        "</TX> \n"};

                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/TX/RETURN_CODE");
                        String tmpRetInfo = tmpBkReader.getFirstText("/TX/RETURN_MSG");
                        if (tmpRetCode != null && tmpRetCode.equals("000000")) {
                            countPage = Integer.parseInt(tmpBkReader.getFirstText("/TX/TX_INFO/TOT_PAGE_NUM"));
                            if (countPage == 0) {
                                retAts.writeRespCode("-1");
                                retAts.writeRespInfo("银行未返回余额对账数据");
                                retAts.writeEnd();
                                return retAts;
                            }
                            while (tmpBkReader.read("/TX/TX_INFO/LIST")) {
                                RetAtsBalCheckDetail tmpRetDetail = new RetAtsBalCheckDetail();
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = tmpRetInfo;
                                tmpRetDetail.accNbr = p_req7508.actNum;
                                //对账单编号
                                tmpRetDetail.statementNumber = tmpBkReader.readedXElement.getSingleText("BILL_NO");
                                //账户名称
                                tmpRetDetail.actName = tmpBkReader.readedXElement.getSingleText("ACCT_NAME");
                                //账户类型 51--对公活期账户 52--对公贷款账户 53--对公定期账户
                                String depType = tmpBkReader.readedXElement.getSingleText("DEP_TYPE");
                                if ("51".equals(depType)) {
                                    tmpRetDetail.acctType = "1";
                                } else if ("52".equals(depType)) {
                                    tmpRetDetail.acctType = "2";
                                } else if ("53".equals(depType)) {
                                    tmpRetDetail.acctType = "3";
                                }
                                //账号
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("ACCT_NO");
                                //币种
                                tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("COIN_TYPE"));
                                //余额
                                tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("CURR_BAL");
                                //对账情况【回签状态 0-回签相符 1-回签不相符 2-未回签】
                                String checkFlag = tmpBkReader.readedXElement.getSingleText("CHECK_FLAG");
                                if ("0".equals(checkFlag)) {
                                    tmpRetDetail.dzFlag = "2";
                                } else if ("1".equals(checkFlag)) {
                                    tmpRetDetail.dzFlag = "3";
                                } else if ("2".equals(checkFlag)) {
                                    tmpRetDetail.dzFlag = "1";
                                }
                                tmpRetDetail.dateFrom = new SimpleDateFormat("yyyyMM").format(dateFrom) + "01";
                                tmpRetDetail.dateTo = dateConvert(dateFrom);
                                //余额核对日期【对账日期】
                                tmpRetDetail.balDate = tmpBkReader.readedXElement.getSingleText("BILL_DATE");
                                tmpRetDetail.yearMonth = tmpBkReader.readedXElement.getSingleText("BILL_DATE").substring(0, 6);
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                            startPage++;
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpSbMsg.toString());
                            break;
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                        break;
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRetBkStr[1]);
                    break;
                }
            } while (startPage <= countPage);
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
}
